package builtin;

import config.Config;
import utils.FileUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SpecialPages {
    private static final Map<Integer, String> buffer = new HashMap<>();

    private static synchronized void loadPage(int code) {
        String path = Config.getConfig().getWebRoot() + "/builtin/" + code + ".html";
        if (code == 0) {
            path = Config.getConfig().getWebRoot() + "/builtin/index.html";
        }

        String content = "page " + code + " not found";
        try {
            content = FileUtils.readFileToString(path);
        } catch(IOException e) {
            e.printStackTrace();
        }

        buffer.put(code, content);
    }

    public static String getPage(int code) {
        if (!buffer.containsKey(code)) {
            loadPage(code); // loadPage is synchronized
        }

        return buffer.get(code);
    }
}
